{include file='common/_meta' /}
<style>
.layui-input-block .layui-form-select .layui-anim-upbit dd
{
    white-space: nowrap;
    max-height: 100%
}
.layui-form-select dl dd, .layui-form-select dl dt {
    white-space: nowrap;
}
</style>
<body>
<div class="container">

    <div class="layui-row">
        <div class="layui-col-md12 breadcrumb">
            <span class="layui-breadcrumb">
                <a><cite>渠道管理菜单</cite></a>
                <a><cite>发送短信</cite></a>
            </span>
        </div>
    </div>

    <div class="layui-fluid" style="background-color: #fff;overflow-x: hidden;">

        <div class="layui-col-xs12 layui-col-md6">
        <div class="layui-row">
            <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                <ul class="layui-tab-title">
                    {if condition="session('user_type') == 'admin' "}
                    <li class="layui-this">外部接口</li>
                    <li>内部接口</li>
                    {/if}
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <form class="layui-form" action="" lay-filter="form1" id="form-article1" enctype="multipart/form-data">

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>运营商：</label>
                                    <div class="layui-input-block">
                                        <select name="ope" id="ope">
                                            <option value=""></option>
                                            {volist name='operators' id='o'}
                                            <option value="{$o.code}"> {$o.desc}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                            </div>

                            {if condition="session('user_type') == 'admin' "}

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>选择渠道：</label>
                                    <div class="layui-input-block">
                                        <select name="cha" id="cha" lay-filter="cha" lay-search>
                                            <option value=""></option>
                                            {volist name='cha' id='c'}
                                            <option value="{$c.channel_id}"> {$c.company_name} ({$c.channel_id})</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                            </div>

                            {/if}

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">是否免审：</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" name="isms" value="1" title="" lay-skin="primary" lay-filter="isms">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row is-no-mianshen">
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label">变量：</label>
                                    <div class="layui-input-block">
                                        <textarea name="bianliang" id="bianliang" class="layui-textarea" oninput="duanxinyl(this)" ></textarea>
                                        <div class="layui-form-mid layui-word-aux custom-textarea-msg">多个变量用 <font class="custom-color-red">|</font> 隔开,注意一定要按顺序对应</div>
                                    </div>
                                 </div>
                            </div>

                            <div class="layui-row is-no-mianshen">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>短信模板：</label>
                                    <div class="layui-input-block">
                                        <select name="notes" id="notes" onchange="duanxinyl(this)" lay-filter="notes">
                                            {volist name='chasmsids' id='sms'}
                                            <option value="{$sms.id}"> {$sms.id} ({$sms.notes})</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row layui-hide is-mianshen">
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>短信内容：</label>
                                    <div class="layui-input-block">
                                        <textarea name="notesnr" rows="6" class="layui-textarea" oninput="duanxinyl(this)"></textarea>
                                    </div>
                                 </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">短信预览：</label>
                                    <div class="layui-input-block">
                                        <p class="duanxinyl" style="background:#F7F5F5;padding:9px 15px;min-height:20px;word-wrap:break-word;word-break: normal;">&nbsp;</p>
                                        <p style="margin-top:5px">
                                        <small>总字数：</small><b><span class="zongzs" style="font-size:1.5rem;color:#FB3535">0</span></b><small>个</small>
                                        &nbsp;&nbsp;
                                        <small>当前短信条数：</small><b><span class="dxtiao" style="font-size:1.5rem;color:#FB3535">0</span></b><small>条</small>
                                        </p>
                                        <p style="color:#FB3535"><small><small>(提示：单条短信按每70个字计费一条，超出70个字的长短信按67个字计费为一条。)</small></small></p>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">文件导入：</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" value="1" title="txt" lay-skin="primary" lay-filter="is_files">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row is-no-files">
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>目标号码：</label>
                                    <div class="layui-input-block">
                                        <textarea name="phones" id="phones" class="layui-textarea" onkeyup="IWS_CheckDecimal(this)"></textarea>
                                    </div>
                                 </div>
                            </div>

                            <div class="layui-row layui-hide is-files">
                                <div class="layui-form-item layui-col-sm12">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>目标号码：</label>
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input uploadfile-name" disabled>
                                    </div>
                                    <div class="layui-input-inline">
                                        <button type="button" class="layui-btn custom-sm-fluid"><i class="layui-icon layui-icon-upload"></i>浏览文件</button>
                                        <input type="file" multiple name="phonesfile" style="position: absolute;top: 0px;left: 0px;bottom: 0px;right: 0px;opacity: 0;filter:alpha(opacity=0);" accept=".txt" class="change-update">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">定时发送：</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" name="is_dingshi" value="1" title="" lay-skin="primary" lay-filter="is_dingshi">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row layui-hide is-dingshitime">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>选择时间：</label>
                                    <div class="layui-input-block">
                                        <input type="text" class="layui-input" name="dingshitime" id="dingshitime1">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <button class="layui-btn custom-sm-fluid" lay-submit lay-filter="formDemo1">发送</button>
                                    </div>
                                </div>
                            </div>

                        </form>
                    </div>
                    <div class="layui-tab-item">

                        {if condition="session('user_type') == 'admin' "}

                        <form class="layui-form" action="" lay-filter="" id="form-article2" enctype="multipart/form-data">

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>运营商：</label>
                                    <div class="layui-input-block">
                                        <select name="ope" id="ope">
                                            <option value=""></option>
                                            {volist name='operators' id='o'}
                                            <option value="{$o.code}"> {$o.desc}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>选择渠道：</label>
                                    <div class="layui-input-block">
                                        <select name="cha" id="cha" lay-search>
                                            <option value=""></option>
                                            {volist name='cha' id='c'}
                                            <option value="{$c.channel_id}"> {$c.company_name} ({$c.channel_id})</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>短信内容：</label>
                                    <div class="layui-input-block">
                                        <textarea name="notesnr" class="layui-textarea" oninput="duanxinyl(this)"></textarea>
                                    </div>
                                 </div>
                            </div>
                            <!-- <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">短信预览：</label>
                                    <div class="layui-input-block">
                                        <p class="duanxinyl" style="background:#F7F5F5;padding:9px 15px;min-height:20px">&nbsp;</p>
                                        <p style="margin-top:5px">
                                        <small>总字数：</small><b><span class="zongzs" style="font-size:1.5rem;color:#FB3535">0</span></b><small>个</small>
                                        &nbsp;&nbsp;
                                        <small>当前短信条数：</small><b><span class="dxtiao" style="font-size:1.5rem;color:#FB3535">0</span></b><small>条</small>
                                        </p>
                                        <p style="color:#FB3535"><small><small>(提示：单条短信按每70个字计费一条，超出70个字的长短信按67个字计费为一条。)</small></small></p>
                                    </div>
                                </div>
                            </div> -->
                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">文件导入：</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" value="1" title="txt" lay-skin="primary" lay-filter="is_files">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row is-no-files">
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>目标号码：</label>
                                    <div class="layui-input-block">
                                        <textarea name="phones" id="phones" class="layui-textarea" onkeyup="IWS_CheckDecimal(this)"></textarea>
                                    </div>
                                 </div>
                            </div>

                            <div class="layui-row layui-hide is-files">
                                <div class="layui-form-item layui-col-sm12">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>目标号码：</label>
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input uploadfile-name" disabled>
                                    </div>
                                    <div class="layui-input-inline">
                                        <button type="button" class="layui-btn custom-sm-fluid"><i class="layui-icon layui-icon-upload"></i>浏览文件</button>
                                        <input type="file" multiple name="phonesfile" style="position: absolute;top: 0px;left: 0px;bottom: 0px;right: 0px;opacity: 0;filter:alpha(opacity=0);" accept=".txt" class="change-update">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>接口路由：</label>
                                    <div class="layui-input-block">
                                        <input type="radio" name="inexe" value="0" title="http">
                                        <input type="radio" name="inexe" value="1" title="cmpp" checked>
                                    </div>
                                 </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">定时发送：</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" name="is_dingshi" value="1" title="" lay-skin="primary" lay-filter="is_dingshi">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row layui-hide is-dingshitime">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="custom-color-red">*</span>选择时间：</label>
                                    <div class="layui-input-block">
                                        <input type="text" class="layui-input" name="dingshitime" id="dingshitime2">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-row">
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <button class="layui-btn custom-sm-fluid" lay-submit lay-filter="formDemo2">发送</button>
                                    </div>
                                </div>
                            </div>

                        </form>

                        {/if}

                    </div>
                </div>
            </div>
        </div>
        </div>
        <div class="layui-col-xs12 layui-col-md6">
        <div class="layui-row" style="padding-top: 10px;">
            <blockquote class="layui-elem-quote">
                <ul>
                    {volist name="send_attentiontxt" id="st"  }
                    <li>{$st|stripslashes}</li>
                    {/volist}
                    <!-- <li>注意事项：</li>
                    <li>* 目标号码手动输入格式：手机号+英文逗号+手机号+...；例子：18813266666,18813266666；</li>
                    <li>* 目标号码文件导入格式：每一行一个号码，文件格式为 txt ；</li>
                    <li>* 短信内容变量用符号：{0}或{1}或... 代替，例：验证码：{0} ，地址：{1} ；</li>
                    <li>* 短信内容变量必须从{0}开始 ，依次递增；</li>
                    <li>* 填写的变量个数必须与模板变量的个数相同 ；</li>
                    <li>* 目标号码最多20000个 ；</li> -->
                    {if condition="session('user_type') == 'admin' "}
                    <li><a href="javascript:;" style="color:#2B22F9" onclick="openedit()">编辑</a></li>
                    {/if}
                </ul>
            </blockquote>
        </div>
        <div class="layui-hide" id="result-msg">
            <div class="layui-fluid" style="padding-top: 30px;">
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>反馈信息</legend>
                    <div class="layui-field-box result-msg" style="padding-left: 20px;padding-right: 20px;color: #333;font-size: 14px;">
                        ...
                    </div>
                </fieldset>
            </div>
        </div>
        </div>
    </div>
</div>
<div hidden>
<textarea id="openedit">{$send_attentiontxt_e}</textarea>
</div>
{include file="common/_footer"}

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript">
//保存的短信
var duanxinylarr = {:json_encode($selectC,JSON_UNESCAPED_UNICODE)};
layui.use(['layer', 'form', 'jquery', 'element', 'laydate'], function() {

    var layer   = layui.layer;
    var form    = layui.form;
    var $       = layui.jquery;
    var element = layui.element;
    var laydate = layui.laydate;

    // 外部接口定时
    laydate.render({
        elem: '#dingshitime1'
        ,type: 'datetime'
        ,min: '{$thistime}'
        ,value: '{$thistime}'
    });

    // 内部接口定时
    laydate.render({
        elem: '#dingshitime2'
        ,type: 'datetime'
        ,min: '{$thistime}'
        ,value: '{$thistime}'
    });

    //是否免审
    form.on('checkbox(isms)', function(data) {

        if (data.elem.checked) {

            $('.is-no-mianshen').addClass('layui-hide');
            $('.is-mianshen').removeClass('layui-hide');
        } else {

            $('.is-no-mianshen').removeClass('layui-hide');
            $('.is-mianshen').addClass('layui-hide');
        }
        duanxinyl(data.elem)
    });

    //是否上传文件
    form.on('checkbox(is_files)', function(data) {

        var parent = $(data.elem).parents('form');

        if (data.elem.checked) {

            parent.find('.is-no-files').addClass('layui-hide');
            parent.find('.is-files').removeClass('layui-hide');
        } else {

            parent.find('.is-no-files').removeClass('layui-hide');
            parent.find('.is-files').addClass('layui-hide');
        }
    });

    //是否定时发送
    form.on('checkbox(is_dingshi)', function(data) {

        var parent = $(data.elem).parents('form');

        if (data.elem.checked) {

            parent.find('.is-dingshitime').removeClass('layui-hide');
        } else {

            parent.find('.is-dingshitime').addClass('layui-hide');
        }
    });

    //选择文件，显示文件名及路径
    $('form').on('change', '.change-update', function(event) {

        $(this).parents('.layui-form-item').find('.uploadfile-name').val(this.value);
    });

    //
    form.on('select(cha)', function(data) {

        findnote(data.value);
    });

    form.on('submit(formDemo1)', function(data) {

        article_submit('#form-article1');
        return false;
    });

    form.on('submit(formDemo2)', function(data) {

        article_submit('#form-article2');
        return false;
    });

    function article_submit(fromtxt) {

        var indexload = layer.load(0);
        var formData = new FormData($(fromtxt)[0]);
        var dingshitime = 0;

        if (fromtxt == '#form-article2') {

            formData.append("isms","5");
        }

        if (formData.get('is_dingshi') == 1) {

            dingshitime = 1;
        }

        $.ajax({
            type : "POST",
            url  : "{:url('SendNote/send_n')}",
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            success : function(e) {

                $('#result-msg').removeClass('layui-hide');
                layer.close(indexload);
                parent.getapplycount_fun();//刷新余额
                $('.result-msg').html(e);

                if (dingshitime == 1) {

                    $(fromtxt)[0].reset();
                    dingshitime = 0;
                }

            },error:function() {

                layer.close(indexload);
                layer.msg('提交失败，请检查网络',{icon: 5,time:1000});
                $dingshitime = 0;
            }
        });

    }

    function findnote(val) {

        if(val) {

            var indexload = layer.load(0);
            $.ajax({
                type : "GET",
                url  : "{:url('SendNote/selectChannel')}?channel_id=" + val,
                dataType:'JSON',
                success : function(e) {

                    layer.close(indexload);
                    var html = '';

                    for(var i in e) {

                        html += '<option value="'+e[i]['id']+'" >'+e[i]['id']+'('+e[i]['notes']+')</option>'

                        duanxinylarr[e[i]['id']] = e[i]['notes2'];
                    }

                    $('#notes').html(html);
                    form.render('select');

                    duanxinyl($('#notes'))
                }, error:function() {

                    layer.close(indexload);
                    $('#notes').html('');
                    form.render('select');
                }
            });


        } else {

            $('#notes').html('');
            form.render('select');
        }
    }

    form.on('select(notes)', function(data){
        duanxinyl(data.elem)
    });
    if("{$exempt}" == "1"){
        $("input[name=isms]").next().click();
    }
});

function openedit()
{
    // layer.open({
    //   type: 1,
    //   content: $('#openedit'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
    //   area: ['500px', '300px']
    // });
    layer.prompt(
        {
            title: "设置注意事项",
            formType: 2, //输入框类型，支持0（文本）默认1（密码）2（多行文本）
            value: $("#openedit").val(), //初始时的值，默认空字符
            maxlength: 1000, //可输入文本的最大长度，默认500
            area: ['500px', '350px'] //自定义文本域宽高
        },
        function(value, index, elem){
            layer.close(index);

            var indexload = layer.load(0);

            $.ajax({
                type : "POST",
                url  : "{:url('SendNote/attentiontxt')}",
                data : {"atxt":value,"type":1},
                success : function(e) {

                    location.reload();

                    layer.msg(e, {icon: 6, time:1000});

                    layer.close(indexload);

                }, error:function() {
                    layer.close(indexload);
                }
            });
        }
    )
}


//短信预览
function duanxinyl(obj)
{
    var tab_content = $(obj).parents(".layui-form");

    tab_content.find(".duanxinyl").text("");
    tab_content.find(".dxtiao").text(0)
    tab_content.find(".zongzs").text(0);
    var txt = '';

    if($("input[name=isms]").is(':checked') )
    {
        txt = tab_content.find("textarea[name=notesnr]").val();
    }else{

        txt = duanxinylarr[tab_content.find("select[name=notes]").val()];

        //短信中的变量
        var re = /{(.*?)}/g;
        var array = [];
        while (temp = re.exec(txt)) {
            array.push(temp[0])
        }
        //变量
        var bianliang = tab_content.find("textarea[name=bianliang]").val().split("|");

        //字符代替
        if(array.length > 0 || bianliang.length > 0)
        {
            for(var i in array)
            {
                if(bianliang[i])
                {
                    txt = txt.replace(array[i],bianliang[i]);
                }
            }
        }

    }

    tab_content.find(".zongzs").text(txt.length);
    if(txt.length > 70){
        tab_content.find(".dxtiao").text(Math.ceil(  (txt.length-70)/67   )+1);
    }else{
        tab_content.find(".dxtiao").text(Math.ceil(  txt.length/70   ));
    }

    tab_content.find(".duanxinyl").text(txt);
}
/*验证textarea目标号码*/
function IWS_CheckDecimal(obj) {

    var temp = /^[0-9,]+$/;
    if (temp.test(obj.value)) {

    } else {

        obj.value = obj.value.substr(0, obj.value.length - 1);
        IWS_CheckDecimal(obj);
    }
}


</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>